package com.cqc.imageswitcher01;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ButtonBarLayout;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

import com.nostra13.universalimageloader.core.ImageLoader;

public class MainActivity extends AppCompatActivity {

    private ImageSwitcher imageSwitcher;
    private Context context = this;
    private Button btn;
    private int[] imgArray = {R.mipmap.a, R.mipmap.b, R.mipmap.c,R.mipmap.d, R.mipmap.e, R.mipmap.f};
    private int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.btn);
        imageSwitcher = (ImageSwitcher) findViewById(R.id.imgSwitch);

        imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                ImageView imageView = new ImageView(context);
                return imageView;
            }
        });
        Animation slide_in = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left);
        Animation slide_out = AnimationUtils.loadAnimation(context, android.R.anim.slide_out_right);
        imageSwitcher.setInAnimation(slide_in);
        imageSwitcher.setOutAnimation(slide_out);
        imageSwitcher.setImageResource(imgArray[0]);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i = (++index) % imgArray.length;
                imageSwitcher.setImageResource(imgArray[i]);
            }
        });
    }
}
